package controller;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import po.Admin;
import po.Level;
import po.Speciality;
import po.Student;
import service.AdminService;
import service.StudentService;
import utils.ResUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StudentController {
    @Autowired
    private StudentService studentService;

    // 获取学生列表数据
    @GetMapping("/students")
    @ResponseBody
    public Object AllUser(HttpServletRequest req) {

        String username = req.getParameter("username").trim();
        String levelId = req.getParameter("level").trim();
        String specialityId = req.getParameter("speciality").trim();
        String status = req.getParameter("status").trim();

        Student student = new Student();
        if (username != "") {
            student.setUsername(username);
        }
        if (levelId != "") {
            Level level = new Level();
            level.setId(Integer.parseInt(levelId));
            student.setLevel(level);
        }
        if (specialityId != "") {
            Speciality speciality = new Speciality();
            speciality.setId(Integer.parseInt(specialityId));
            student.setSpeciality(speciality);
        }
        if (status != "") {
            student.setIsEnable(new Byte(status));
        }
//        System.out.println(username + " "+levelId +" "+specialityId + status +"获取到的值");

        List<Student> res = this.studentService.allStudent(student);

        if (res != null) {
            return ResUtils.Success(1,"查询成功",res);
        } else {
            return ResUtils.Fail(0,"没有数据");
        }

    }

    // 删除学生
    @DeleteMapping("students/{id}")
    @ResponseBody
    public Object deleteStudent(@PathVariable("id") Integer id) {
        boolean res = studentService.deleteStudent(id);

        if (res) {
            return ResUtils.Success(1,"删除成功");
        } else {
            return ResUtils.Fail(0,"删除失败");
        }
    }

    // 增加学生
    @PostMapping("students/")
    @ResponseBody
    public Object addStudent(@RequestBody Student student) {
        Date now = new Date();
        student.setCreateTime(now);
        student.setUpdateTime(now);
        System.out.println(student);
        boolean res = studentService.addStudent(student);

        if (res) {
            return ResUtils.Success(1,"添加成功",student);
        } else {
            return ResUtils.Fail(0,"添加失败");
        }
    }

    // 根据 id 查询学生信息
    @GetMapping("/students/{id}")
    @ResponseBody
    public Object studentOfId(@PathVariable("id") Integer id) {

//        System.out.println("获取id为" + id + "客户对象");
        Student student = studentService.studentOfId(id);

        if (student!=null) {
            return ResUtils.Success(1, "查询成功",student);
        } else {
            return ResUtils.Fail(0, "查询失败不存在 Id 为 "+id+" 的学生信息");
        }
    }

    // 根据 id 修改学生信息
    @PutMapping("/students/{id}")
    @ResponseBody
    public Object editStudent(@PathVariable("id") Integer id,@RequestBody Student student) {

        Date now = new Date();
        student.setUpdateTime(now);

        System.out.println(student);

        Boolean res = studentService.editStudent(student);

        if (res) {
            return ResUtils.Success(1, "修改成功",student);
        } else {
            return ResUtils.Fail(0, "修改失败");
        }
    }

    // 根据 id 修改状态学生信息
    @PutMapping("/students/status/{id}")
    @ResponseBody
    public Object isEnableStudent(@PathVariable("id") Integer id,@RequestParam("isEnable") Integer isEnable) {
//        System.out.println(id+"接收到的id " +isEnable);
        Integer flag = isEnable == 1 ? 0 : 1;
        Boolean res = studentService.isEnableStudent(id,flag);
        if (res) {
            return ResUtils.Success(1, "状态成功");
        } else {
            return ResUtils.Fail(0, "状态失败");
        }
    }
}
